<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Permission_m extends Base_model {
	public $primary_key = 'permission_id';
	
	function __construct() {
        parent::__construct();
    }
	
	function get_by_sidebar()
	{
		$this->load->model('group_permissions_m');
		$permissions = $this->permission->get_many_by('permission_is_sidebar',1);
		$result = array();
		$result['groups'] = array();
		$result['permissions'] = array();
		if($permissions)
		{
		   $groups = array();
		   $permission_groups = array();
		   foreach($permissions as $permission)
		   {
		   	if(has_permission($permission->permission_name))
		   	{
		   		array_push($groups, $permission->pgroup_id);
			  	$result['permissions'][$permission->pgroup_id][] = $permission;
		   	}
			  
		   }
		   if($groups)
		   {
			 $groups = $this->group_permissions_m->where_in($groups)->order_by('pgroup_order')->get_many_by();
			 if($groups)
			 {
				foreach($groups as $group)
				{
					 if(isset($result['permissions'][$group->pgroup_id]))
					 {
						$result['groups'][] = $group;
					 }
					// $result[$group->pgroup_id][] = 
				}
			 }
		   }
		}
		return $result;
	}
}

?>